//
//  BkGoodsDetailViewController.m
//  TuDianEducation
//
//  Created by lixinkeji on 2021/1/13.
//  Copyright © 2021 zhangbenchao. All rights reserved.


#import "BkGoodsDetailViewController.h"
#import "BkAgeListViewController.h"
#import "BkCommitOrderViewController.h"
#import "BkMoreCommedViewController.h"
#import "BkAuthorDetailViewController.h"

#import "BkGoodsCollectionViewCell.h"
#import "BkGoodsBuyBottomView.h"
#import "LXMarketGoodsListModel.h"

#import "BkGoodsDetailHeaderView.h"
#import "BkShareAlertViewController.h"

@interface BkGoodsDetailViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UITextFieldDelegate>{
    CGFloat left;
    CGFloat space;
    
}
@property (nonatomic,strong) UICollectionView *collectionView;
@property (nonatomic,assign) NSInteger row;

@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, assign) NSInteger pageNum;
@property (nonatomic,strong) BkGoodsDetailHeaderView *headerView;
@property (nonatomic,strong) BkGoodsBuyBottomView *bottomView;
///详情数据
@property (nonatomic,strong) NSDictionary *dataDic;
@property (nonatomic,strong) NSString *shareUrl;

@end

@implementation BkGoodsDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.row = 2;
    [self.view addSubview:self.collectionView];
   
    //[self.collectionView.mj_header beginRefreshing];
    self.view.backgroundColor = kSubBacColor;
    
    [self.view addSubview:self.bottomView];
    self.title = @"商品详情";
    [self navigationConfig];
    if (kLogin) {
        [self getgoodsdetail];
        [self userInforRequstAction];
    }else{
        [MBProgressHUD showError:@"请先登录"];
    }
    //[self getgoodsdetail];
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}
-(BkGoodsBuyBottomView *)bottomView{
    if (!_bottomView) {
        _bottomView = [[BkGoodsBuyBottomView alloc]init];
        _bottomView.top = _collectionView.bottom;
        WS(weakSelf);
        _bottomView.buyBlock = ^{
            BkCommitOrderViewController *vc = [[BkCommitOrderViewController alloc]init];
            vc.dataDic = weakSelf.dataDic;
            [weakSelf naviPushVc:vc];
        };
        _bottomView.saveBlock = ^{
            [weakSelf requstaddgoodscoll];
        };
        _bottomView.carBlock = ^{
            [weakSelf getaddgoodscarWithGid:weakSelf.gid];
        };
        _bottomView.serverBlock = ^{
            [weakSelf userInforRequstGetbuttonsAction];
        };
    }
    return _bottomView;
}

-(void)userInforRequstGetbuttonsAction{
    
        [NetWorkTools postConJSONWithUrl:@"getbuttons" parameters:@{} success:^(id responseObject) {
            NSString *result = responseObject[@"result"];
            NSString *resultNote = responseObject[@"resultNote"];
            NSString *datastr = responseObject[@"datastr"];
            NSString *url  = kKefuServerUrl;
            if ([result integerValue] == 0) {
                if ( [datastr integerValue] == 0) {
                    url = kKefuServerUrl;
                }else{
                    url = kSelfServerUrl;
                }
                [self gotoWebWithUrl:url title:@"我的客服"];
            }else{
                 [MBProgressHUD showError:resultNote];
            }
                         
        } fail:^(NSError *error) {
            
        }];
}
-(void)navigationConfig{
    [self addRightNavgationItemWithImage:[UIImage imageNamed:@"分享"]];
   
}


#pragma mark --分享事件
-(void)rigthBtnAction:(id)sender{
    BkShareAlertViewController *vc = [[BkShareAlertViewController alloc]init];
    vc.dataDic = self.dataDic;
    vc.modalPresentationStyle = UIModalPresentationOverFullScreen;
     [self.navigationController presentViewController:vc animated:YES completion:^{
         //
         vc.view.superview.backgroundColor = [UIColor clearColor];
         if (self.shareUrl.length) {
             [TSWLTools shareBtnClickWithImg:nil withTitle:@"" withContent:@"欢迎使用戴胜鸟图书" withWebUrl:self.shareUrl];
         }
     }];
    
}
-(void)userInforRequstAction{
    if (kLogin) {
        [NetWorkTools postConJSONWithUrl:@"memberinfo" parameters:@{} success:^(id responseObject) {
            NSString *result = responseObject[@"result"];
            NSString *resultNote = responseObject[@"resultNote"];
            NSDictionary *data = responseObject[@"dataobject"];
            if ([result integerValue] == 0) {
                NSString *  invitationcode = data[@"invitationcode"];
               
                self.shareUrl = [NSString stringWithFormat:@"http://app.daishengbook.com/h5/#/pages/detail/index?invitationcode=%@&gid=%@",invitationcode,self.dataDic[@"gid"]];
            }else{
                 [MBProgressHUD showError:resultNote];
            }
                         
        } fail:^(NSError *error) {
            
        }];
    }
   
    
}
-(BkGoodsDetailHeaderView *)headerView{
    if (!_headerView) {
        _headerView = [[BkGoodsDetailHeaderView alloc]init];
        _headerView.top = -_headerView.height-TSScaleW(10);
        WS(weakSelf);
        _headerView.moreConmendBlock = ^{
            BkMoreCommedViewController *vc = [[BkMoreCommedViewController alloc]init];
            vc.gid = weakSelf.gid;
            vc.title = [NSString stringWithFormat:@"商品评论（%@）",weakSelf.dataDic[@"commnum"]];
            [weakSelf naviPushVc:vc];
        };
        _headerView.authorBlock = ^{
            BkAuthorDetailViewController *vc = [BkAuthorDetailViewController alloc];
            vc.aid = weakSelf.dataDic[@"aid"];
            [weakSelf naviPushVc:vc];
        };
        _headerView.reloadFrameBlock = ^{
            weakSelf.collectionView.contentOffset = CGPointMake(0, -weakSelf.headerView.height-TSScaleW(10));
            weakSelf.collectionView.contentInset = UIEdgeInsetsMake(weakSelf.headerView.height + TSScaleW(10), TSScaleW(15), TSScaleW(10), TSScaleW(15));
            weakSelf.headerView.top = -weakSelf.headerView.height-TSScaleW(10);
            [weakSelf.collectionView addSubview:weakSelf.headerView];
          
        };
    }
    return _headerView;
}
-(UICollectionView *)collectionView
{
    if (!_collectionView) {
        
           left = TSScaleW(15);
           space = TSScaleW(10);
           _row = _row?:4;
           UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
           layout.itemSize = CGSizeMake((self.view.width - left * 2 - space * (_row - 1))/_row - 1,TSScaleW(233));
           layout.minimumLineSpacing = space;
           layout.minimumInteritemSpacing = space;
           layout.scrollDirection = UICollectionViewScrollDirectionVertical;
           
           
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, TSScreenWidth, TSScreenHeight - Height_NavBar- TSScaleW(50)) collectionViewLayout:layout];
           _collectionView.delegate = self;
           _collectionView.dataSource = self;
           [_collectionView registerClass:[BkGoodsCollectionViewCell class] forCellWithReuseIdentifier:@"BkGoodsCollectionViewCell"];
        _collectionView.contentInset = UIEdgeInsetsMake(self.headerView.height + TSScaleW(10), left, TSScaleW(10), left);
           _collectionView.scrollEnabled = YES;
           _collectionView.backgroundColor = kSubBacColor;
        _collectionView.contentOffset = CGPointMake(0, -self.headerView.height-TSScaleW(10));
        //手势添加
        //此处给其增加长按手势，用此手势触发cell移动效果
        [self.view addSubview:_collectionView];
        [_collectionView addSubview:self.headerView];
        WS(weakSelf);
        _collectionView.mj_header = [MJRefreshHeader headerWithRefreshingBlock:^{
            [weakSelf getgoodsdetail];
        }];

    }
    return _collectionView;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    BkGoodsDetailViewController *vc = [[BkGoodsDetailViewController alloc]init];
    NSDictionary *dic = self.dataArray[indexPath.row];
    vc.gid = dic[@"gid"];
    [self.navigationController pushViewController:vc animated:YES];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
   
    return self.dataArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
        
    BkGoodsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"BkGoodsCollectionViewCell" forIndexPath:indexPath];
    cell.contentView.backgroundColor = kWhiteColor;
    cell.dataDic = self.dataArray[indexPath.row];
    NSDictionary *dic = self.dataArray[indexPath.row];
    WS(weakSelf);
    cell.addCarBlock = ^{
        [weakSelf getaddgoodscarWithGid:dic[@"gid"]];
    };
    return cell;
}






-(NSArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

#pragma mark ----商品详情请求
-(void)getgoodsdetail
{
    NSDictionary * pamas = @{@"gid":_gid};
    [NetWorkTools postConJSONWithUrl:@"getgoodsdetail" parameters:pamas success:^(id responseObject) {
        NSString *result = responseObject[@"result"];
        NSString *resultNote = responseObject[@"resultNote"];
        NSArray *array = responseObject[@"dataList"];
        NSDictionary*dataobject = responseObject[@"dataobject"];
        if (result.integerValue == 0) {
            [self.dataArray removeAllObjects];
            [self.dataArray addObjectsFromArray:array];
            self.dataDic = dataobject;
            [self.collectionView reloadData];
            
        }else{
             [MBProgressHUD showError:resultNote];
        }
        [self.collectionView.mj_header endRefreshing];
    } fail:^(NSError *error) {
        [self.collectionView.mj_header endRefreshing];
    }];

}
-(void)setDataDic:(NSDictionary *)dataDic{
    _dataDic = dataDic;
    self.headerView.dataDic = _dataDic;
    NSString *iscoll = _dataDic[@"iscoll"];
    self.bottomView.saveBtn.selected = iscoll.intValue;
}
#pragma mark ---商品收藏
-(void)requstaddgoodscoll{
    NSDictionary * pamas = @{@"gid":_dataDic[@"gid"]};
    [NetWorkTools postConJSONWithUrl:@"addgoodscoll" parameters:pamas success:^(id responseObject) {
        NSString *result = responseObject[@"result"];
        NSString *resultNote = responseObject[@"resultNote"];
      
        if (result.integerValue == 0) {
            self.bottomView.saveBtn.selected = !self.bottomView.saveBtn.selected ;
        }else{
            
        }
        [MBProgressHUD showError:resultNote];
    } fail:^(NSError *error) {
        
    }];
}
#pragma mark ----添加购物车

-(void)getaddgoodscarWithGid:(NSString *)gid
{
    ///默认一个
    if (![self ifNoLoginGotoLoginAction]) {
        return;
    }
    NSDictionary * pamas = @{@"gid":gid,@"numbers":@"1"};
    [NetWorkTools postConJSONWithUrl:@"addgoodscar" parameters:pamas success:^(id responseObject) {
        NSString *result = responseObject[@"result"];
        NSString *resultNote = responseObject[@"resultNote"];
       
        if (result.integerValue == 0) {
            
        }else{
            
        }
        [MBProgressHUD showError:resultNote];
    } fail:^(NSError *error) {
        
    }];
}

@end
